import baseapi from '@/api/base'
import { getDataByParameter, getCountByParameter, saveObjetInfo, deleteObjetInfo } from '@/utils/baserequest'
import storage from 'store'

const Base64 = require('js-base64').Base64

const api = {
  accountServer: '/account/',
  accountByPersonPid: '/account/by/personPid/',
  accountCountByPersonPid: '/account/countBy/personPid/',
  accountRest: '/account/restwd/',
  accountChangePasswd: '/account/changepwd/',
  // orgPid
  orgPid: storage.get('org')
}

export default api

// 得到部门成员 分页
export function getAccountByPersonPid (parameter, headers, pageSize, current) {
  const appIdHeaders = {
    appId: baseapi.huiwcore_app,
    org: storage.get('org'),
    authOption: '1'
  }
  const parameters = {
    personPid: parameter,
    pageSize: pageSize,
    currentPage: current
  }
  const url = baseapi.baseUrl + baseapi.huiwcore + api.accountByPersonPid
  const allheaders = Object.assign(headers, appIdHeaders)
  return getDataByParameter(url, parameters, allheaders)
  // .then(res => {
  //   return res
  // })
}

// 得到人员账号总数
export function getAccountCountByPersonPid (parameter, headers) {
  const appIdHeaders = {
    appId: baseapi.huiwcore_app,
    org: storage.get('org'),
    authOption: '1'
  }
  const url = baseapi.baseUrl + baseapi.huiwcore + api.accountCountByPersonPid
  const allheaders = Object.assign(headers, appIdHeaders)
  return getCountByParameter(url, parameter, allheaders)
}

// id == 0 add     post
// id != 0 update  put
// 保存账号信息
export function saveAccountInfo (parameter, headers) {
  const appIdHeaders = {
    appId: baseapi.huiwcore_app,
    org: storage.get('org')
  }
  const data = {
    sysSeq: 0,
    accountType: 'passwd'
  }
  parameter.accountPasswd = Base64.encode(parameter.accountPasswd)
  const url = baseapi.baseUrl + baseapi.huiwcore + api.accountServer
  const allheaders = Object.assign(headers, appIdHeaders)
  return saveObjetInfo(url, Object.assign(parameter, data), allheaders)
}

// 删除账号信息
export function delAccount (parameter, headers) {
  const appIdHeaders = {
    appId: baseapi.huiwcore_app,
    org: storage.get('org')
  }
  const url = baseapi.baseUrl + baseapi.huiwcore + api.accountServer // + '/'
  const allheaders = Object.assign(headers, appIdHeaders)
  return deleteObjetInfo(url, parameter, allheaders)
}

// 重置密码
export function restAccount (parameter, headers) {
  const appIdHeaders = {
    appId: baseapi.huiwcore_app,
    org: storage.get('org')
  }
  parameter.newAccountPasswd = Base64.encode(parameter.newAccountPasswd)
  const url = baseapi.baseUrl + baseapi.huiwcore + api.accountRest // + '/'
  const allheaders = Object.assign(headers, appIdHeaders)
  return saveObjetInfo(url, parameter, allheaders)
}

// 修改密码
export function changeAccount (parameter, headers) {
  const appIdHeaders = {
    appId: baseapi.huiwcore_app,
    org: storage.get('org')
  }
  console.log(parameter)
  parameter.newAccountPasswd = Base64.encode(parameter.newAccountPasswd)
  parameter.oldAccountPasswd = Base64.encode(parameter.oldAccountPasswd)
  console.log(parameter)
  const url = baseapi.baseUrl + baseapi.huiwcore + api.accountChangePasswd // + '/'
  const allheaders = Object.assign(headers, appIdHeaders)
  return saveObjetInfo(url, parameter, allheaders)
}
